package interfata;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JWindow;

import command.AfisareCommand;
import command.ArgDependentCommand;
import command.CalculCommand;
import command.Command;
import command.DerivSimbolicaCommand;
import command.DerivareCommand;
import command.DesenareArboreCommand;
import command.GraficFunctieCommand;

import command.IncarcareXML;
import command.SaveArboreXML;
import arbori.ArboreFunctional;

public class MainFrame extends JFrame implements ActionListener{

	JPanel panel;
	
	JTextField valXTF;
	
	JTextField functieTF;
	JTextField derivataTF;
	JTextField calculTF;
	JTextField derivataSimbTF;
	
	AfisareCommand afisare;
	CalculCommand calcul;
	DerivareCommand derivare;
	DerivSimbolicaCommand derivSimbolica;
	SaveArboreXML sxml;
	IncarcareXML ixml;
	DesenareArboreCommand darbore;
	GraficFunctieCommand grafic;
	
	JLabel labelsalvare;
	JLabel labelload;
	JTextField fisiersalvare;
	JTextField fisierload;
	
	ArboreFunctional functie;
	
	public MainFrame() {
		super("COMMAND");
		
		//this.functie = functie;
		ArgDependentCommand.setFunctie(functie);
		
		//panel = new JPanel(new BorderLayout());
		
		valXTF = new JTextField();
		functieTF = new JTextField();
		derivataTF = new JTextField();
		calculTF = new JTextField();
		derivataSimbTF = new JTextField();
		
		
		JLabel adFuncLabel= new JLabel();
		JLabel valXLabel= new JLabel();
		JLabel calculLabel= new JLabel();
		JLabel derivataLabel= new JLabel();
		JLabel derSimbLabel= new JLabel();
		
		//functieTF.setEnabled(false);
		//derivataTF.setEnabled(false);
		//calculTF.setEnabled(false);
		//derivataSimbTF.setEnabled(false);
		
		panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		
		/**
		 * Functia f(x)
		 */
		adFuncLabel= new JLabel("f(x) = ");
		adFuncLabel.setBounds(20, 50, 50, 20);
		panel.add(adFuncLabel);
		
		functieTF.setBounds(100, 50, 150, 20);
		panel.add(functieTF);
		
		/**
		 * punctul in care se fac calculele
		 */
		valXLabel=new JLabel("x = ");
		valXLabel.setBounds(20, 80, 80, 20);
		panel.add(valXLabel);
		
		valXTF.setBounds(100, 80, 150, 20);	
		panel.add(valXTF);
		
		/**
		 * afisarea rezultatelor calculelor
		 */
		calculLabel=new JLabel("Calculul: ");
		calculLabel.setBounds(20, 110, 80, 20);
		panel.add(calculLabel);
		
		calculTF.setBounds(100, 110, 150, 20);
		panel.add(calculTF);
		
		derivataLabel=new JLabel("Derivata: ");
		derivataLabel.setBounds(20, 140, 80, 20);
		panel.add(derivataLabel);
		
		derivataTF.setBounds(100, 140, 150, 20);
		panel.add(derivataTF);
		
		derSimbLabel=new JLabel("Derivata Simbolica: ");
		derSimbLabel.setBounds(20, 170, 130, 20);
		panel.add(derSimbLabel);
		
		derivataSimbTF.setBounds(130, 170, 150, 20);
		panel.add(derivataSimbTF);
		
		/**
		 * butoanele de calculare, derivare, derivare simbolica si afisare a functiei
		 */
		
		afisare = new AfisareCommand("Afisaza Functia", functieTF);
		calcul = new CalculCommand("Calculeaza",calculTF);
		derivare = new DerivareCommand("Deriveaza", derivataTF);
		derivSimbolica = new DerivSimbolicaCommand("Derivare Simbolica",derivataSimbTF);
		
		afisare.addActionListener(this);
		calcul.addActionListener(this);
		derivare.addActionListener(this);
		derivSimbolica.addActionListener(this);
		
		afisare.setBounds(20, 330, 150, 20);
		calcul.setBounds(400, 90, 150, 20);
		derivare.setBounds(400, 120, 150, 20);
		derivSimbolica.setBounds(400, 150, 150, 20);
		
		
		panel.add(afisare);
		panel.add(calcul);
		panel.add(derivare);
		panel.add(derivSimbolica);
		
		/**
		 * Salvare xml
		 */
		labelsalvare = new JLabel("Salvare XML in:");
		labelsalvare.setBounds(20, 240, 150, 20);
		//labelsalvare.setForeground(Color.white);
		panel.add(labelsalvare);
		
		fisiersalvare = new JTextField();
		//fisiersalvare.setBackground(Color.gray);
		//fisiersalvare.setForeground(Color.yellow);
		fisiersalvare.setBounds(20, 260, 150, 20);
		panel.add(fisiersalvare);	
		
		sxml=new SaveArboreXML("Salvare XML");
		sxml.setBounds(180, 260, 150, 20);
		panel.add(sxml);
		sxml.addActionListener(this);
		
		/**
		 * Incarcare xml
		 */
		labelload = new JLabel("Fisierul din care citim:");
		labelload.setBounds(20, 280, 150, 20);
		//labelload.setForeground(Color.white);
		panel.add(labelload);
		
		fisierload = new JTextField();
		//fisierload.setBackground(Color.gray);
		//fisierload.setForeground(Color.yellow);
		fisierload.setBounds(20, 300, 150, 20);
		panel.add(fisierload);	

		ixml=new IncarcareXML("Incarcare XML");
		ixml.setBounds(180, 300, 150, 20);
		panel.add(ixml);
		ixml.addActionListener(this);
		
		/**
		 * Desenare arbore
		 */
		
		darbore=new DesenareArboreCommand("Desen Arbore");
		darbore.setBounds(400, 200, 170, 20);
		panel.add(darbore);
		darbore.addActionListener(this);
		/**
		 * Graficul functiei
		 */
		grafic=new GraficFunctieCommand("Graficul functiei");
		grafic.setBounds(400, 230, 170, 20);
		panel.add(grafic);
		grafic.addActionListener(this);
		
		
		
		pack();
		setSize(600,400);
		setResizable(false);
		//setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void afiseaza() {

		setVisible(true);
	}

	/**
	 * functia care apeleaza metodele corespunzatoare fiecarui buton
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof ArgDependentCommand) {
			try {
				double x = Double.parseDouble(valXTF.getText());
				ArgDependentCommand cmd = (ArgDependentCommand)e.getSource();
				//System.out.println(x);
				cmd.setValX(x);
				//cmd.execute();
				
				String ft= functieTF.getText();
				System.out.println(ft);
				//cmd = (ArgDependentCommand)e.getSource();
				cmd.setFunctie(ft);
				functie=cmd.getFunctie();
				cmd.execute();
				
			} catch (NumberFormatException nfx) {
				JOptionPane.showMessageDialog(this, 
						"Introduceti argumentul x!", 
						"Eroare", 
						JOptionPane.ERROR_MESSAGE);
			}
		} 
		if (e.getSource().equals(sxml))
		{
			
			sxml.setArbore(functie.getRadacina());
			try {
				sxml.setFisier(fisiersalvare.getText());
				sxml.execute();
			}catch(Exception exc)
			{
				
			}
			
		}
		if (e.getSource().equals(ixml))
		{
			try{
			ixml.setFisier(fisierload.getText());
			ixml.execute();
			}catch(Exception ex){}
			functie=new ArboreFunctional(ixml.getArbore());
			
		}
		if (e.getSource().equals(darbore))
		{
			darbore.setArbore(functie.getRadacina());
			darbore.execute();
		}
		if (e.getSource().equals(grafic))
		{
			grafic.setArbore(functie.getRadacina());
			grafic.execute();
		}
		else {
		Command cmd = (Command)e.getSource();
		cmd.execute();
	}
}

}
